package org.nobody.mallb2c.mallcart.controller;

import org.nobody.mallb2c.mallcart.interceptor.CartInterceptor;
import org.nobody.mallb2c.mallcart.service.CartService;
import org.nobody.mallb2c.mallcart.to.UserInfoTo;
import org.nobody.mallb2c.mallcart.vo.Cart;
import org.nobody.mallb2c.mallcart.vo.CartItem;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import java.util.List;
import java.util.concurrent.ExecutionException;

/**
 * @author 3489309323@qq.com
 * @create 2020-09-19-18:58
 */
@Controller
public class CartController {
  @Autowired
  CartService cartService;

  @GetMapping("/currentUserCartItems")
  @ResponseBody
  public List<CartItem> getCurrentUserCartItems(){

    return cartService.getUserCartItems();
  }

  @GetMapping("/deleteItem")
  public String deleteItem(
    @RequestParam("skuId") Long skuId
  ){
    cartService.deleteItem(skuId);
    return "redirect:http://cart.mall.com/cart.html";
  }

  /**
   * 增加购物项
   * @param skuId
   * @param num
   * @return
   */
  @GetMapping("/countItem")
  public String countItem(
    @RequestParam("skuId") Long skuId,
    @RequestParam("num") Integer num
  ){
    cartService.changeItemCount(skuId, num);
    return "redirect:http://cart.mall.com/cart.html";
  }

  /**
   * 勾选购物项
   * @return
   */
  @GetMapping("/checkItem")
  public String checkItem(
    @RequestParam("skuId") Long skuId,
    @RequestParam("check") Integer check
  ){
    cartService.checkItem(skuId, check);

    return "redirect:http://cart.mall.com/cart.html";
  }

  /**
   * 去购物车页面
   *
   * 浏览器有一个cookie(无论是否登录):
   * --> user-key,用来标识用户身份(临时用户身份),一个月后过期
   *
   * 每次访问都会带上这个user-key cookie
   *
   * 登录: session有
   * 没登录: 就按照cookie里面带的user-key来做
   *
   *
   * @return
   */
  @GetMapping("/cart.html")
  public String cartListPage(Model model) throws ExecutionException, InterruptedException {
    UserInfoTo userInfoTo = CartInterceptor.threadLocal.get();
    //System.out.println("userInfoTo:" + userInfoTo);
    Cart cart = cartService.getCart();
    model.addAttribute("cart", cart);

    return "cartList";
  }

  /**
   * 添加商品到购物车
   * @return
   */
  @GetMapping("addToCart")
  public String addToCart(
    @RequestParam("skuId") Long skuId,
    @RequestParam("num") Integer num,
    RedirectAttributes ra
  ) throws ExecutionException, InterruptedException {
    cartService.addToCart(skuId,num);

    /*
      注意和addFlashAttribute相区别
      addFlashAttribute是靠session实现的(一次性的)

      addAttribute是拼接query
    * */
    ra.addAttribute("skuId", skuId);

    return "redirect:http://cart.mall.com/addToCartSuccess.html";
  }

  /**
   * 跳转到成功页
   * @param skuId
   * @param model
   * @return
   */
  @GetMapping("/addToCartSuccess.html")
  public String addToCartSuccessPage(@RequestParam("skuId") Long skuId,Model model){
    CartItem item = cartService.getCartItem(skuId);
    model.addAttribute("item", item);
    return "success";
  }
}
